<?php

use Phinx\Migration\AbstractMigration;

class AddModuleRelationData extends AbstractMigration
{
    /**
     * 获取所有模块数据
     * @return array
     */
    public function getAllModuleData()
    {
        $sql = 'select * from module';
        $result = $this->query($sql);
        $info = array();
        foreach ($result as $key => $val) {
            $info[$val['code']] = $val;
        }
        return $info;
    }

    /**
     * Migrate Up.
     * @throws Exception
     */
    public function up()
    {
        $moduleMap = $this->getAllModuleData();

        // 已经存在的关联模型
        $schemaConfig = [
            [
                'schema' => [
                    'code' => 'tenant',
                ],
                'module_relation' => [
                    [
                        'src_module_code' => 'tenant',
                        'dst_module_code' => 'tenant_user',
                        'type' => 'has_many',
                        'link_id' => ['tenant_id']
                    ]
                ]
            ],
            [
                'schema' => [
                    'code' => 'tenant_user',
                ],
                'module_relation' => [
                    [ // 关联租户
                        'src_module_code' => 'tenant_user',
                        'dst_module_code' => 'user',
                        'type' => 'belong_to',
                        'link_id' => ['user_id']
                    ],
                    [
                        'src_module_code' => 'tenant_user',
                        'dst_module_code' => 'role',
                        'type' => 'belong_to',
                        'link_id' => ['role_id']
                    ],
                    [
                        'src_module_code' => 'tenant_user',
                        'dst_module_code' => 'department',
                        'type' => 'belong_to',
                        'link_id' => ['department_id']
                    ]
                ]
            ],
        ];

        // 写入数据库
        $moduleRelationTable = $this->table('module_relation');

        foreach ($schemaConfig as $schemaItem) {

            $sql = 'SELECT max(`id`) as id FROM `schema` where code="' . $schemaItem['schema']['code'] . '"';
            // 获取当前写入schema id
            $query = $this->fetchRow($sql);

            foreach ($schemaItem["module_relation"] as $moduleRelationItem) {
                $moduleRelationData = $this->generateNodeConfig($query['id'], $moduleRelationItem, $moduleMap);
                $moduleRelationTable->insert($moduleRelationData)->save();
            }
        }
    }

    /**
     * 生成关联节点配置
     * @param $schemaId
     * @param $moduleRelationConfig
     * @param $moduleMap
     * @return array
     * @throws Exception
     */
    public function generateNodeConfig($schemaId, $moduleRelationConfig, $moduleMap)
    {
        $config = [
            'type' => $moduleRelationConfig['type'],
            'src_module_id' => $moduleMap[$moduleRelationConfig['src_module_code']]['id'],
            'dst_module_id' => $moduleMap[$moduleRelationConfig['dst_module_code']]['id'],
            'link_id' => join(',', $moduleRelationConfig['link_id']),
            'schema_id' => $schemaId,
            'uuid' => Webpatser\Uuid\Uuid::generate()->string,
            'node_config' => json_encode([
                "edges" => [
                    "data" => [
                        "type" => "connection",
                        "label" => $moduleRelationConfig['type']
                    ],
                    "source" => $moduleMap[$moduleRelationConfig['src_module_code']]['uuid'],
                    "target" => $moduleMap[$moduleRelationConfig['dst_module_code']]['uuid']
                ],
                "node_data" => [
                    "source" => [
                        "h" => "80px",
                        "w" => "120px",
                        "id" => $moduleMap[$moduleRelationConfig['src_module_code']]['uuid'],
                        "top" => "147px",
                        "left" => "405px",
                        "name" => ucfirst($moduleRelationConfig['src_module_code']),
                        "type" => "module",
                        "icon" => "",
                        "module_id" => $moduleMap[$moduleRelationConfig['src_module_code']]['id'],
                        "module_code" => $moduleRelationConfig['src_module_code'],
                        "module_type" => $moduleMap[$moduleRelationConfig['src_module_code']]['type']
                    ],
                    "target" => [
                        "h" => "80px",
                        "w" => "120px",
                        "id" => $moduleMap[$moduleRelationConfig['dst_module_code']]['uuid'],
                        "top" => "387px",
                        "left" => "229px",
                        "name" => ucfirst($moduleRelationConfig['dst_module_code']),
                        "type" => "module",
                        "icon" => "",
                        "module_id" => $moduleMap[$moduleRelationConfig['dst_module_code']]['id'],
                        "module_code" => $moduleRelationConfig['dst_module_code'],
                        "module_type" => $moduleMap[$moduleRelationConfig['dst_module_code']]['type']
                    ]
                ]
            ])
        ];

        return $config;
    }
}
